<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Gift;
use Illuminate\Http\Request;
use View;
use App\Services\RedisService;

class GiftController extends Controller
{
	

	/**
	 * Display a listing of the resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function index() 
	{		
		$where = $param = [];

		$list = Gift::paginate(20);		
		return view('admin.gift.index', compact('list','param','where'));
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function create(Request $request) 
    {	
		return view('admin.gift.add');
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @return \Illuminate\Http\Response
	 */
	public function store(Request $request) 
    {
		$data = $request->except('_token');
		$data = array_map('trim', $data);
		$data['code'] = str_random(8);
		$rs   = Gift::create($data);
		$key = 'h5:gift:'.$rs->id;
		RedisService::set($key,$rs->toJson());
		$keyNum ='h5:gift:num:'.$rs->id;
		RedisService::set($keyNum,$data['num']);
		if ($rs->id) {
			return back()->with('rs', ['flag' => 'success', 'msg' => '操作成功']);
		} else {
			return back()->with('rs', ['flag' => 'warning', 'msg' => '操作失败']);
		}
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function show($id) {
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function edit($id) {
		$data = Gift::find($id);
		return view('admin.gift.edit',compact('data'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function update(Request $request, $id) {
		$rs['status'] = false;
		$data = $request->except(['_token', '_method']);
		$data = array_map('trim', $data);

		$flag = Gift::where('id',$id)->update($data);
		
		$redisDATA = Gift::find($id);
		$key = 'h5:gift:'.$id;
		RedisService::set($key,$redisDATA->toJson());
		$keyNum ='h5:gift:num:'.$id;
		RedisService::set($keyNum,$data['num']);

		if ($flag) {
			$rs['status'] = true;
			$rs['msg'] = '操作成功';
			return redirect('zadmin/gift')->with('rs', $rs);
		} else {
			$rs['msg'] = '操作失败';
			return back()->with('rs', $rs);
		}
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function destroy($id)
	{
		Gift::destroy($id);
		$key = 'h5:gift:'.$id;
		RedisService::del($key);
		$keyNum ='h5:gift:num:'.$id;
		RedisService::del($keyNum);
		return back();
	}

	public function infoToRedis(Request $request)
	{
		$list = Gift::all();
		foreach($list as $v) {
			$key = 'h5:gift:'.$v->id;
			RedisService::set($key,$v->toJson());
		}
		return back();
	}

	public function numToRedis(Request $request)
	{
		$list = Gift::all();
		foreach($list as $v) {
			//dd($v);
			$key = 'h5:gift:num:'.$v->id;
			RedisService::set($key,$v->num);
		}
		return back();
	}
}
